package geek;

import org.junit.jupiter.api.Test;

import java.util.PriorityQueue;

/**
 * @author sam
 * 2021/10/19 23:06
 * @description
 * @editor
 * @description
 *
 * 数组中第k大元素
 */
public class Lesson11 {

    @Test
    public void kMaxValueTest(){
        Integer[] values = {1,4,6,0,3,6,7};
        int k = 3;
        System.out.println(kMaxValue(k,values).toString());
    }

    private Integer[] kMaxValue(int k,Integer[] values){
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        for (Integer value : values) {
            if (priorityQueue.size()<k){
                priorityQueue.add(value);
            }else if (priorityQueue.peek()<value){
                priorityQueue.poll();
                priorityQueue.add(value);
            }
        }
        return priorityQueue.toArray(new Integer[k]);

    }

}


